package com.wd.visitor.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static com.wd.visitor.common.constants.ApiConstant.BASE_API;
import static com.wd.visitor.common.constants.ApiConstant.BASE_IMG_API;

/**
 * @className: HttpUtils
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/
public class HttpUtils {


    public static void main(String[] args) {
        String urlString = "https://your.api.endpoint"; // 替换为你的URL
        String jsonInputString = "{\"key\": \"value\"}"; // 替换为你的JSON数据

        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
//            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);

            // 发送请求
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            // 可以在这里添加代码处理响应

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String post(String urlString, String param) {

        String result = "";
        try {
            URL url = new URL(BASE_API + urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
//            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);

            connection.setDoOutput(true);
            connection.setDoInput(true);
            OutputStreamWriter out = null;
            BufferedReader in = null;
            // 发送请求

            out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            out.write(param);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                }
                result += line;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void get(String urlString) {

        try {
            URL url = new URL(BASE_IMG_API + urlString);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // 设置请求方法
            con.setRequestMethod("GET");

            // 获取响应码
            int responseCode = con.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印结果
            System.out.println("Response: " + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
